<template>
  <div class="container">
    <div class="page-header">
      <h1>📝 用户注册</h1>
    </div>

    <div class="form-container">
      <a-form 
        :model="registerForm" 
        :rules="rules" 
        ref="registerFormRef"
        layout="vertical"
        size="large"
      >
        <a-form-item label="用户名" field="username">
          <a-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
            allow-clear
          >
            <template #prefix>
              <icon-user />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item label="邮箱" field="email">
          <a-input
            v-model="registerForm.email"
            placeholder="请输入邮箱地址"
            allow-clear
          >
            <template #prefix>
              <icon-email />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item label="密码" field="password">
          <a-input-password
            v-model="registerForm.password"
            placeholder="请输入密码"
            allow-clear
          >
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item label="确认密码" field="confirmPassword">
          <a-input-password
            v-model="registerForm.confirmPassword"
            placeholder="请再次输入密码"
            allow-clear
          >
            <template #prefix>
              <icon-lock />
            </template>
          </a-input-password>
        </a-form-item>

        <a-form-item label="验证码" field="captcha">
          <a-input-group>
            <a-input
              v-model="registerForm.captcha"
              placeholder="请输入验证码"
              allow-clear
              style="flex: 1;"
            />
            <a-button 
              type="primary" 
              :disabled="captchaLoading || captchaCountdown > 0"
              @click="sendCaptcha"
              style="min-width: 100px;"
            >
              {{ getCaptchaButtonText }}
            </a-button>
          </a-input-group>
        </a-form-item>

        <a-form-item field="agree">
          <a-checkbox v-model="registerForm.agree">
            我已阅读并同意
            <a-link>《用户协议》</a-link>
            和
            <a-link>《隐私政策》</a-link>
          </a-checkbox>
        </a-form-item>

        <a-form-item>
          <a-button 
            type="primary" 
            long
            size="large"
            :loading="loading"
            @click="handleRegister"
          >
            {{ loading ? '注册中...' : '立即注册' }}
          </a-button>
        </a-form-item>
      </a-form>

      <div class="footer-links">
        <p style="text-align: center; color: #666; margin: 20px 0;">
          已有账号？
          <a-link @click="$router.push('/login')">
            立即登录
          </a-link>
        </p>
      </div>
    </div>

    <div class="btn-group" style="margin-top: 30px;">
      <button class="nav-btn secondary" @click="$router.push('/')">
        返回首页
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { Message } from '@arco-design/web-vue'
import { IconUser, IconEmail, IconLock } from '@arco-design/web-vue/es/icon'

const router = useRouter()
const registerFormRef = ref()
const loading = ref(false)
const captchaLoading = ref(false)
const captchaCountdown = ref(0)

const registerForm = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  captcha: '',
  agree: false
})

const validatePassword = (value, callback) => {
  if (!value) {
    callback('请输入密码')
  } else if (value.length < 6) {
    callback('密码长度不能少于6位')
  } else {
    callback()
  }
}

const validateConfirmPassword = (value, callback) => {
  if (!value) {
    callback('请再次输入密码')
  } else if (value !== registerForm.password) {
    callback('两次输入密码不一致')
  } else {
    callback()
  }
}

const validateAgree = (value, callback) => {
  if (!value) {
    callback('请阅读并同意用户协议')
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名' },
    { minLength: 3, maxLength: 20, message: '用户名长度在 3 到 20 个字符' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址' },
    { type: 'email', message: '请输入正确的邮箱地址' }
  ],
  password: [
    { validator: validatePassword }
  ],
  confirmPassword: [
    { validator: validateConfirmPassword }
  ],
  captcha: [
    { required: true, message: '请输入验证码' }
  ],
  agree: [
    { validator: validateAgree }
  ]
}

const getCaptchaButtonText = computed(() => {
  if (captchaLoading.value) return '发送中...'
  if (captchaCountdown.value > 0) return `${captchaCountdown.value}s`
  return '获取验证码'
})

const sendCaptcha = async () => {
  if (!registerForm.email) {
    Message.warning('请先输入邮箱地址')
    return
  }

  captchaLoading.value = true
  
  // 模拟发送验证码
  setTimeout(() => {
    captchaLoading.value = false
    captchaCountdown.value = 60
    Message.success('验证码已发送到您的邮箱')
    
    // 倒计时
    const timer = setInterval(() => {
      captchaCountdown.value--
      if (captchaCountdown.value <= 0) {
        clearInterval(timer)
      }
    }, 1000)
  }, 1000)
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  try {
    await registerFormRef.value.validate()
    loading.value = true
    
    // 模拟注册请求
    setTimeout(() => {
      loading.value = false
      Message.success('注册成功！请登录')
      router.push('/login')
    }, 2000)
    
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}
</script>

<style scoped>
.form-container {
  background: white;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.footer-links {
  border-top: 1px solid #eee;
  padding-top: 20px;
}
</style> 